package com.doyled.jump.scene;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.naming.ldap.Rdn;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

import com.doyled.jump.util.ChessKind;
import com.doyled.jump.util.PlayerMode;
import com.doyled.jump.util.UserSetting;

public class ChoosePlayersScene extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField jtfPlayerName;
	private JTextField jtfPlayer1Name;
	private JTextField jtfPlayer2Name;

	private int numberOfPlayers = 4;
	private PlayerMode mode = PlayerMode.Single;
	private String playerName = "Player";
	private int playerColor = 0;
	private String player1Name = "Player 1";
	private int player1Color = 0;
	private String player2Name = "Player 2";
	private int player2Color = 1;
	private boolean isOk = false;

	public boolean isOk() {
		return isOk;
	}

	/**
	 * Create the dialog.
	 */
	public ChoosePlayersScene() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setModal(true);
		setTitle("Choosing Players");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
		{
			JPanel panel = new JPanel();
			panel.setAlignmentX(Component.LEFT_ALIGNMENT);
			contentPanel.add(panel);
			panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

			JLabel lblNewLabel = new JLabel("Number of Players: ");
			lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
			panel.add(lblNewLabel);

			JRadioButton rdbtnNo2 = new JRadioButton("2");
			rdbtnNo2.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					JRadioButton source = (JRadioButton) e.getSource();
					if (source.isSelected() == true) {
						numberOfPlayers = 2;
					}
				}
			});
			rdbtnNo2.setHorizontalAlignment(SwingConstants.LEFT);
			rdbtnNo2.setSelected(true);
			panel.add(rdbtnNo2);

			JRadioButton rdbtnNo3 = new JRadioButton("3");
			rdbtnNo3.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					JRadioButton source = (JRadioButton) e.getSource();
					if (source.isSelected() == true) {
						numberOfPlayers = 3;
					}
				}
			});
			rdbtnNo3.setHorizontalAlignment(SwingConstants.LEFT);
			panel.add(rdbtnNo3);

			JRadioButton rdbtnNo4 = new JRadioButton("4");
			rdbtnNo4.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					JRadioButton source = (JRadioButton) e.getSource();
					if (source.isSelected() == true) {
						numberOfPlayers = 4;
					}
				}
			});
			rdbtnNo4.setHorizontalAlignment(SwingConstants.LEFT);
			panel.add(rdbtnNo4);

			ButtonGroup group1 = new ButtonGroup();
			group1.add(rdbtnNo2);
			group1.add(rdbtnNo3);
			group1.add(rdbtnNo4);
		}
		{
			JPanel panel = new JPanel();
			panel.setAlignmentX(Component.LEFT_ALIGNMENT);
			contentPanel.add(panel);
			panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
			{
				JLabel lblNewLabel_1 = new JLabel("User Infomation:");
				lblNewLabel_1.setHorizontalAlignment(SwingConstants.LEFT);
				panel.add(lblNewLabel_1);
			}
			{
				JPanel panel_1 = new JPanel();
				panel_1.setAlignmentX(Component.LEFT_ALIGNMENT);
				panel.add(panel_1);
				panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));

				JRadioButton rdbtnSinglePlayer = new JRadioButton(
						"Single Player");
				rdbtnSinglePlayer.addChangeListener(new ChangeListener() {
					public void stateChanged(ChangeEvent e) {
						JRadioButton source = (JRadioButton) e.getSource();
						if (jtfPlayerName == null)
							return;
						jtfPlayerName.setEnabled(source.isSelected());
						if (source.isSelected() == true) {
							mode = PlayerMode.Single;
						}
					}
				});
				rdbtnSinglePlayer.setSelected(true);
				rdbtnSinglePlayer.setHorizontalAlignment(SwingConstants.LEFT);
				panel_1.add(rdbtnSinglePlayer);

				JPanel singlePlayerInfoPanel = new JPanel();
				singlePlayerInfoPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
				panel_1.add(singlePlayerInfoPanel);
				singlePlayerInfoPanel.setLayout(new BoxLayout(
						singlePlayerInfoPanel, BoxLayout.Y_AXIS));
				{
					JPanel panel_3 = new JPanel();
					singlePlayerInfoPanel.add(panel_3);
					panel_3.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
					{
						JLabel lblPlayerName = new JLabel("Player Name: ");
						lblPlayerName
								.setHorizontalAlignment(SwingConstants.CENTER);
						panel_3.add(lblPlayerName);
					}
					{
						jtfPlayerName = new JTextField();
						jtfPlayerName
								.setHorizontalAlignment(SwingConstants.CENTER);
						panel_3.add(jtfPlayerName);
						jtfPlayerName.setColumns(10);
					}
				}
				{
					JLabel label = new JLabel("");
					singlePlayerInfoPanel.add(label);
				}

				JRadioButton rdbtnTwoPlayer = new JRadioButton("2-Player");
				rdbtnTwoPlayer.addChangeListener(new ChangeListener() {
					public void stateChanged(ChangeEvent e) {
						JRadioButton source = (JRadioButton) e.getSource();
						if (jtfPlayer1Name == null || jtfPlayer2Name == null)
							return;
						jtfPlayer1Name.setEnabled(source.isSelected());
						jtfPlayer2Name.setEnabled(source.isSelected());
						if (source.isSelected() == true) {
							mode = PlayerMode.two_Player;
						}
					}
				});
				rdbtnTwoPlayer.setHorizontalAlignment(SwingConstants.LEFT);
				panel_1.add(rdbtnTwoPlayer);

				JPanel twoPlayerInfoPanel = new JPanel();
				panel_1.add(twoPlayerInfoPanel);
				twoPlayerInfoPanel.setLayout(new BoxLayout(twoPlayerInfoPanel,
						BoxLayout.Y_AXIS));
				{
					JPanel player1Panel = new JPanel();
					player1Panel.setAlignmentX(Component.LEFT_ALIGNMENT);
					twoPlayerInfoPanel.add(player1Panel);
					player1Panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5,
							5));
					{
						JLabel lblPlayerName_1 = new JLabel("Player 1 Name: ");
						lblPlayerName_1
								.setHorizontalAlignment(SwingConstants.CENTER);
						player1Panel.add(lblPlayerName_1);
					}
					{
						jtfPlayer1Name = new JTextField();
						jtfPlayer1Name.setEnabled(false);
						jtfPlayer1Name
								.setHorizontalAlignment(SwingConstants.CENTER);
						jtfPlayer1Name.setColumns(10);
						player1Panel.add(jtfPlayer1Name);
					}
				}
				{
					JPanel player2Panel = new JPanel();
					player2Panel.setAlignmentX(Component.LEFT_ALIGNMENT);
					twoPlayerInfoPanel.add(player2Panel);
					player2Panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5,
							5));
					{
						JLabel lblPlayerName_2 = new JLabel("Player 2 Name: ");
						lblPlayerName_2
								.setHorizontalAlignment(SwingConstants.CENTER);
						player2Panel.add(lblPlayerName_2);
					}
					{
						jtfPlayer2Name = new JTextField();
						jtfPlayer2Name.setEnabled(false);
						jtfPlayer2Name
								.setHorizontalAlignment(SwingConstants.CENTER);
						jtfPlayer2Name.setColumns(10);
						player2Panel.add(jtfPlayer2Name);
					}
				}

				ButtonGroup group2 = new ButtonGroup();
				group2.add(rdbtnSinglePlayer);
				group2.add(rdbtnTwoPlayer);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ButtonProcessing());
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ButtonProcessing());
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	class ButtonProcessing implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String command = e.getActionCommand();
			if (command.equals("OK")) {
				isOk = true;
				Random random = new Random();
				if (mode == PlayerMode.Single) {
					playerName = jtfPlayerName.getText();
					// random chess color
					playerColor = random.nextInt(4);
					// show message
					String messageText = playerName + " owns " + getColorChess(playerColor) + " Farm";
					JOptionPane.showMessageDialog(null, messageText);
				} else {
					player1Name = jtfPlayer1Name.getText();
					player2Name = jtfPlayer2Name.getText();
					// random chess color
					player1Color = random.nextInt(4);
					player2Color = random.nextInt(4);
					if (player2Color == player1Color) {
						player2Color = (++player2Color) % 4;
					}
					// show message
					String messageText = player1Name + " owns " + getColorChess(player1Color) + " Farm. \n" + 
									player2Name + " owns " + getColorChess(player2Color) + " Farm. \n";
					JOptionPane.showMessageDialog(null, messageText);
				}
				// set user setting
				UserSetting.numberOfPlayers = numberOfPlayers;
				UserSetting.mode = mode;
				UserSetting.playerName = playerName;
				UserSetting.playerColor = playerColor;
				UserSetting.player1Name = player1Name;
				UserSetting.player1Color = player1Color;
				UserSetting.player2Name = player2Name;
				UserSetting.player2Color = player2Color;
			}
			ChoosePlayersScene.this.dispose();
		}
		
		private String getColorChess(int i){
			String colorChess = "";
			switch (i) {
			case 0:
				colorChess = "Red";
				break;
			case 1:
				colorChess = "Blue";
				break;
			case 2:
				colorChess = "Yellow";
				break;
			case 3:
				colorChess = "Green";
				break;
			default:
				break;
			}
			return colorChess;
		}

	}

}
